/*
 *	OpenSource code and fragments by avocado systems, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'avocaod systems, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 */
package de.fuhrmeister.browserchooser.view;

import java.awt.event.MouseListener;
import java.util.EventListener;
import java.util.Iterator;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 * The popup for the main view.
 * 
 * @date 18.12.2008
 * @author Marcus Fuhrmeister
 */
public class DockletPopupMenuView extends JPopupMenu {

	private static final long serialVersionUID = 5438935651321451354L;
	private JMenuItem menuItemNew;
	private JMenuItem menuItemLanguageLoad;
	private JMenuItem menuItemSettings;
	private JMenuItem menuItemExit;
	private JMenu menuLanguage;
	private JMenuItem menuItemDefaultLanguage;

	/**
	 * Sets the "new" menu item.
	 * 
	 * @param text the "new" menu item value
	 */
	public void setItemNew(final String text) {
		menuItemNew = new JMenuItem(text);
		this.add(menuItemNew);
	}

	/**
	 * Sets the "language" menu item.
	 * 
	 * @param text the "language" menu item value
	 */
	public void setMenuLanguage(final String text) {
		menuLanguage = new JMenu(text);
		this.add(menuLanguage);
	}

	/**
	 * Sets the "default language" menu item.
	 * 
	 * @param text the "default language" menu item value
	 */
	public void setItemDefaultLanguage(final String text) {
		menuItemDefaultLanguage = new JMenuItem(text);
		menuLanguage.add(menuItemDefaultLanguage);
	}

	/**
	 * Adds the "language" menu items as sub menu items to the "language" menu item.
	 * 
	 * @param i the string iterator of the languages
	 * @param ml the MouseListener
	 */
	public void setItemsLanguage(final Iterator<String> i, final MouseListener ml) {
		if (!i.hasNext()) {
			menuItemDefaultLanguage.setEnabled(false);
		}
		while (i.hasNext()) {
			final JMenuItem menuItemLanguage = new JMenuItem(i.next());
			menuItemLanguage.addMouseListener(ml);
			menuLanguage.add(menuItemLanguage);
		}
	}

	/**
	 * Sets the "load language" menu item.
	 * 
	 * @param text  the "load language" menu item value
	 */
	public void setItemLanguageLoad(final String text) {
		menuItemLanguageLoad = new JMenuItem(text);
		menuLanguage.addSeparator();
		menuLanguage.add(menuItemLanguageLoad);
	}
	/**
	 * Sets the "settings" menu item.
	 * 
	 * @param text  the "settings" menu item value
	 */
	public void setItemSettings(final String text) {
		menuItemSettings = new JMenuItem(text);
		this.add(menuItemNew);
	}

	/**
	 * Sets the "exit" menu item.
	 * 
	 * @param text  the "exit" menu item value
	 */
	public void setItemExit(final String text) {
		menuItemExit = new JMenuItem(text);
		this.addSeparator();
		this.add(menuItemExit);
	}

	/**
	 * Registers the listeners.
	 * 
	 * @param ml the EventListener
	 */
	public void registerListener(final EventListener ml) {
		if (ml instanceof MouseListener) {
			menuItemNew.addMouseListener((MouseListener) ml);
			menuItemDefaultLanguage.addMouseListener((MouseListener) ml);
			menuItemLanguageLoad.addMouseListener((MouseListener) ml);
			menuItemSettings.addMouseListener((MouseListener) ml);
			menuItemExit.addMouseListener((MouseListener) ml);
		}
	}
}
